import json
import logging
import traceback
from datetime import datetime
from django.db import transaction
from django.forms import model_to_dict
from django.http import JsonResponse
from rest_framework import status
from Model.models import Subdistrict as S, Community as C, Address as A
from Model.modelsView import SubDistrictInfo
from tools.Tools import check_text, getCurrentUser, createHandleLog

logger_info = logging.getLogger('info')
res = dict()


def querySubDistrictByDistrict(request):
    res.clear()
    try:
        if request.method == 'GET':
            district_id = request.GET.get('district_id')
            if check_text(district_id):

                sub = S.objects.filter(district_id=district_id)
                sub = list(sub.values())
            else:
                sub = []
            res['status'] = 200
            res['data'] = sub
            res['message'] = '请求成功'
            return JsonResponse(res, status=status.HTTP_200_OK)
    except Exception as e:
        print(e)
        logger_info.error(f'当前接口：{request.path} 异常，异常内容为:{traceback.format_exc()}')
        res['status'] = 200
        res['message'] = '请求错误!'
        return JsonResponse(res, status=status.HTTP_400_BAD_REQUEST)


def queryCommunityBySubDistrict(request):
    res.clear()
    try:
        if request.method == 'GET':
            subdistrict_id = request.GET.get('subdistrict_id')
            if check_text(subdistrict_id):
                com = C.objects.filter(subdistrict_id=subdistrict_id)
                com = list(com.values())
            else:
                com = []
            res['status'] = 200
            res['data'] = com
            res['message'] = '请求成功'
            return JsonResponse(res, status=status.HTTP_200_OK)
    except Exception as e:
        print(e)
        logger_info.error(f'当前接口：{request.path} 异常，异常内容为:{traceback.format_exc()}')
        res['status'] = 200
        res['message'] = '请求错误!'
        return JsonResponse(res, status=status.HTTP_400_BAD_REQUEST)


def queryAddressByCommunity(request):
    res.clear()
    try:
        if request.method == 'GET':
            community_id = request.GET.get('community_id')
            if check_text(community_id):
                address = A.objects.filter(community_id=community_id)
                address = list(address.values())
            else:
                address = []
            res['status'] = 200
            res['data'] = address
            res['message'] = '请求成功'
            return JsonResponse(res, status=status.HTTP_200_OK)
    except Exception as e:
        print(e)
        logger_info.error(f'当前接口：{request.path} 异常，异常内容为:{traceback.format_exc()}')
        res['status'] = 200
        res['message'] = '请求错误!'
        return JsonResponse(res, status=status.HTTP_400_BAD_REQUEST)


def queryCommunityByName(request):
    res.clear()
    try:
        if request.method == 'GET':
            name = request.GET.get('name')
            if check_text(name):
                com = C.objects.filter(name__contains=name)
                com = list(com.values())
            else:
                com = []
            res['status'] = 200
            res['data'] = com
            res['message'] = '请求成功'
            return JsonResponse(res, status=status.HTTP_200_OK)
    except Exception as e:
        print(e)
        logger_info.error(f'当前接口：{request.path} 异常，异常内容为:{traceback.format_exc()}')
        res['status'] = 200
        res['message'] = '请求错误!'
        return JsonResponse(res, status=status.HTTP_400_BAD_REQUEST)


def queryAddressByName(request):
    res.clear()
    try:
        if request.method == 'GET':
            name = request.GET.get('name')
            if check_text(name):
                address = A.objects.filter(name__contains=name)
                address = list(address.values())
            else:
                address = []
            res['status'] = 200
            res['data'] = address
            res['message'] = '请求成功'
            return JsonResponse(res, status=status.HTTP_200_OK)
    except Exception as e:
        print(e)
        logger_info.error(f'当前接口：{request.path} 异常，异常内容为:{traceback.format_exc()}')
        res['status'] = 200
        res['message'] = '请求错误!'
        return JsonResponse(res, status=status.HTTP_400_BAD_REQUEST)


def getAddressById(request):
    res.clear()
    try:
        if request.method == 'GET':
            address_id = request.GET.get('address_id')
            if check_text(address_id):
                address = A.objects.get(address_id=address_id)
                address = model_to_dict(address)
            else:
                address = {}
            res['status'] = 200
            res['data'] = address
            res['message'] = '请求成功'
            return JsonResponse(res, status=status.HTTP_200_OK)
    except Exception as e:
        print(e)
        logger_info.error(f'当前接口：{request.path} 异常，异常内容为:{traceback.format_exc()}')
        res['status'] = 200
        res['message'] = '请求错误!'
        return JsonResponse(res, status=status.HTTP_400_BAD_REQUEST)


def getCommunityById(request):
    res.clear()
    try:
        if request.method == 'GET':
            community_id = request.GET.get('community_id')
            if check_text(community_id):
                com = C.objects.get(community_id=community_id)
                com = model_to_dict(com)
            else:
                com = []
            res['status'] = 200
            res['data'] = com
            res['message'] = '请求成功'
            return JsonResponse(res, status=status.HTTP_200_OK)
    except Exception as e:
        print(e)
        logger_info.error(f'当前接口：{request.path} 异常，异常内容为:{traceback.format_exc()}')
        res['status'] = 200
        res['message'] = '请求错误!'
        return JsonResponse(res, status=status.HTTP_400_BAD_REQUEST)
